Connected vs. Disconnected Architecture in ADO.NET
In ADO.NET, there are two types of architectures for accessing and managing data:
- Connected Architecture (Uses
DataReader) - Disconnected Architecture (Uses
DataSet)
Connected Architecture (Using DataReader)
Best for: Fast, read-only access to data with an active database connection.
How it Works?
- Establishes a connection to the database.
- Uses
SqlCommandandSqlDataReaderto retrieve data. - Reads data row-by-row (forward-only).
- The connection remains open while reading data.
- After reading, the connection must be closed manually.
Example: Using DataReader (Connected Mode)
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connStr = "Server=your_server;Database=your_db;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open(); // Must keep connection open
SqlCommand cmd = new SqlCommand("SELECT ID, Name FROM Employees", conn);
SqlDataReader reader = cmd.ExecuteReader(); // Executes query
while (reader.Read()) // Reads row by row (Forward-only)
{
Console.WriteLine($"ID: {reader["ID"]}, Name: {reader["Name"]}");
}
reader.Close(); // Close reader
} // Connection automatically closes here due to 'using'
}
}
Pros:
- Fast and efficient for large datasets.
- Uses less memory (does not store data in memory).
- Best for read-only operations.
Cons:
- Requires an active connection to read data.
- Cannot modify or store data in memory.
- Cannot navigate backward (forward-only).
Disconnected Architecture (Using DataSet)
Best for: Working with data offline without an active database connection.
How it Works?
- Uses
SqlDataAdapterto fetch data without keeping the connection open. - Stores data in a DataSet (in-memory storage).
- Allows sorting, filtering, and modifying data.
- Can update changes back to the database using
SqlDataAdapter.Update().
Example: Using DataSet (Disconnected Mode)
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connStr = "Server=your_server;Database=your_db;Integrated Security=True;";
DataSet ds = new DataSet(); // Stores data in-memory
using (SqlConnection conn = new SqlConnection(connStr))
{
string query = "SELECT ID, Name FROM Employees";
SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
adapter.Fill(ds, "Employees"); // Loads data into DataSet
}
// No active connection required after fetching data
foreach (DataRow row in ds.Tables["Employees"].Rows)
{
Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}");
}
}
}
Pros:
- No need to keep the connection open.
- Can work with multiple tables (like an in-memory database).
- Supports modifications, filtering, and sorting.
- Can update data back to the database.
Cons:
- Slower than
DataReader(stores data in memory). - Uses more memory for large datasets.
Key Differences: Connected vs. Disconnected Architecture
| Feature | Connected (DataReader) |
Disconnected (DataSet) |
|---|---|---|
| Connection Type | Always open | Works without an active connection |
| Data Storage | Reads row-by-row | Stores entire data in memory |
| Navigation | Forward-only | Can navigate, filter, and sort |
| Performance | Faster, low memory usage | Slower, higher memory usage |
| Data Modification | No (Read-only) | Yes (Can modify and update) |
| Best for | Fast reading, large datasets | Disconnected apps, multiple tables |
When to Use Which?
| Scenario | Use DataReader (Connected) |
Use DataSet (Disconnected) |
|---|---|---|
| Large datasets (fast, read-only) | Yes | No |
| Need to modify data before saving | No | Yes |
| Need sorting/filtering in-memory | No | Yes |
| Need to store multiple tables | No | Yes |
| Requires real-time data | Yes | No |
Summary
| Architecture | Use When... |
|---|---|
Connected (DataReader) |
You need fast, read-only, forward-only access to data. |
Disconnected (DataSet) |
You need offline access, store multiple tables, or modify data. |
Leave Comment